package com.ha8.netease.views.team.userlist
{
	import com.ha8.netease.App;
	import com.ha8.netease.PrivateNotify;
	import com.ha8.netease.models.WeiboProxy;
	import com.ha8.netease.models.vo.UsersReqVO;
	import com.ha8.netease.models.vo.ViewModel;
	import com.ha8.netease.views.utils.ObjectUtil;
	import com.rogo.mvc.interfaces.INotification;
	import com.rogo.mvc.sub.mediator.Mediator;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	/**
	 * 用户列表页面视图代理
	 * 好友 粉丝
	 * @author rogo
	 */	
	public class UserListMediator extends Mediator
	{
		public function UserListMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'userListMediator';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var model:ViewModel;
		private var userVO:UsersReqVO;
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			view.addEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
			
			displayUser();
		}
		
		override public function listNotificationInterests():Array
		{
			return [PrivateNotify.GET_FRIENDS_RESULT,
				PrivateNotify.GET_FRIENDS_ERROR,
				PrivateNotify.GET_FANS_RESULT,
				PrivateNotify.GET_FANS_ERROR]
		}
		
		override public function notificationHandler(notification:INotification):void
		{
			switch (notification.name)
			{
				case PrivateNotify.GET_FRIENDS_RESULT:
				case PrivateNotify.GET_FANS_RESULT:
					view.dataGroup.dataProvider = notification.param as ArrayCollection;
					ObjectUtil.traceObj(notification.param);
					break;
				case PrivateNotify.GET_FRIENDS_ERROR:
				case PrivateNotify.GET_FANS_ERROR:
					break;
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function get view():UserListView
		{
			return viewComponent as UserListView;
		}
		
		public function get proxy():WeiboProxy
		{
			return facade.retrieveProxy(WeiboProxy.NAME) as WeiboProxy;
		}
		
		/**
		 * 显示用户数据
		 */		
		public function displayUser():void
		{
			if (model &&
				model.equal(App.getInstance().userListModel))
				return;
			
			model = App.getInstance().userListModel;
			userVO = new UsersReqVO();
			userVO.user_id = model.user.id;
			
			switch (model.type)
			{
				case ViewModel.FRIENDS:
					if (model.user.id == App.getInstance().me.id)
						view.title = '我关注的人';
					else
						view.title = '他关注的人';
					break;
				case ViewModel.FANS:
					if (model.user.id == App.getInstance().me.id)
						view.title = '关注我的人';
					else
						view.title = '关注他的人';
					break;
			}
			
			requestData();
		}
		
		/**
		 * 开始请求数据
		 */		
		private function requestData():void
		{
			switch (model.type)
			{
				case ViewModel.FRIENDS:
					proxy.getFriends(userVO);
					break;
				case ViewModel.FANS:
					proxy.getFans(userVO);
					break;
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function addToStageHandler(e:Event):void
		{
			displayUser();	
		}
		
	}
}